package com.travel.server.manager.impl.command.project.data;

import com.travel.common.CommonConstant;
import com.travel.common.excel.ExcelData;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.common.util.ExcelUtil;
import com.travel.server.manager.dto.request.project.data.ExportProjectCostRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.dto.response.project.data.QueryProjectCostResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.ProjectDal;
import com.travel.server.manager.impl.dal.SpecialDal;
import com.travel.server.manager.impl.sal.ProjectCostSal;
import com.travel.server.manager.model.ProjectSpecial;
import com.travel.server.manager.model.dto.data.OrderCostItemDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/29 12:12
 * @Desc:
 */
@Component
@Slf4j
public class ExportProjectCostCommand extends AbstractCommand<ExportProjectCostRequest, EmptyResponse> {

    @Autowired
    private SpecialDal specialDal;

    @Autowired
    private ProjectDal projectDal;

    @Override
    protected void checkInput(ExportProjectCostRequest request) {
        checkTimeInput(request);
    }

    @Override
    public EmptyResponse handle(ExportProjectCostRequest request) {
        checkInput(request);

        List<String> title = Arrays.asList("项目名", "出行人数", "出行日期", "项目费收入", "申请费收入", "成本合计", "合作方结算", "优惠", "赔偿", "税费", "保险", "领队差旅", "物资费用", "备注");
        List<List<Object>> rows = new ArrayList<>();

        Date startTime = DateUtil.getDataTime(request.getStartTime());
        Date endTime = DateUtil.getDataEndTime(request.getEndTime());

        List<QueryProjectCostResponse.CostItem> costList = new ArrayList<>();

        List<ProjectSpecial> specialList = specialDal.selectSpecial(startTime, endTime, null, null);
        if (CollectionUtil.isEmpty(specialList)) {
            throw new WorkException(WorkCode.EXCEL_DATA_NULL);
        }
        List<OrderCostItemDto> costItemList = specialDal.selectOrderCostItem(startTime, endTime);
        Map<Integer, Integer> projectMap = ProjectCostSal.makeCostMap(costItemList, 1);
        Map<Integer, Integer> applyMap = ProjectCostSal.makeCostMap(costItemList, 2);
        Map<Integer, Integer> discountMap = ProjectCostSal.makeCostMap(costItemList, 3);
        Map<Integer, Integer> countMap = ProjectCostSal.makeCostMap(costItemList, 4);
        Map<Integer, String> nameMap = projectDal.makeProjectName(specialList);
        for (ProjectSpecial projectSpecial : specialList) {
            List<Object> list = new ArrayList<>();
            Integer id = projectSpecial.getId();
            Integer countId = countMap.get(id);
            countId = countId == null ? 0 : countId;
            Long orderProject = countId * projectSpecial.getPrice();
            Long project = projectSpecial.getProject() + orderProject;
            Integer orderApply = countId * projectSpecial.getApplyAmount();
            Integer apply = projectSpecial.getApply() + orderApply;
            Integer discount = discountMap.get(id);
            discount = (discount == null ? 0 : discount) + projectSpecial.getDiscount();
            list.add(nameMap.get(projectSpecial.getProjectId()));
            list.add(countId + "");
            list.add(DateUtil.dateToStr(projectSpecial.getTravelDate(), DateUtil.YYYY_MM_DD));
            list.add(CommonUtil.fenToYuan(project).toString());
            list.add(CommonUtil.fenToYuan(apply).toString());
            list.add(projectSpecial.getPartner());
            list.add(discount);
            list.add(projectSpecial.getCompensate());
            list.add(projectSpecial.getTaxation());
            list.add(projectSpecial.getInsurance());
            list.add(projectSpecial.getTravel());
            list.add(projectSpecial.getMaterial());
            list.add(projectSpecial.getRemark() == null ? CommonConstant.EMPTY_STR : projectSpecial.getRemark());
            rows.add(list);
        }

        ExcelData excelData = new ExcelData();
        excelData.setName("成本统计");
        excelData.setTitles(title);
        excelData.setRows(rows);
        ExcelUtil.exportExcel(getHttpServletResponse(), "成本统计", excelData);
        return new EmptyResponse();
    }
}
